package com.future.mayi.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.BrandModel;
import com.future.mayi.model.interfaces.IndexBrandClickListener;
import com.future.mayi.utils.HCUtils;

import java.util.List;

public class IndexBrandView extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private LayoutInflater layoutInflater;
    private int brandItemW;
    private int brandImgItemW;
    private int brandImgItemH;

    public IndexBrandView(Context context) {
        super(context);
        init(context);
    }

    public IndexBrandView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IndexBrandView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setPadding(CommonUtils.dip2px(context, 6.0f), 0, 0, 0);
        mContext = context;
        layoutInflater = LayoutInflater.from(context);
        setOrientation(HORIZONTAL);
        brandItemW = (int) (((float) CommonUtils.getScreenSize(context)[0] - CommonUtils.dip2px(context, 46.5f)) / 4f);
        brandImgItemW = brandItemW - CommonUtils.dip2px(context, 12.0f);
        //brandImgItemH = (int) ((float) brandImgItemW / 70.0f * 38.5f);
        brandImgItemH = brandImgItemW;
    }


    private List<BrandModel> brandModels;


    public void setBrand(List<BrandModel> brandModels) {
        if (this.brandModels != null && brandModels != null) {
            if (TextUtils.equals(App.getGson().toJson(this.brandModels), App.getGson().toJson(brandModels))) {
                return;
            }
        }
        this.brandModels = brandModels;
        if (brandModels == null || brandModels.size() == 0) {
            removeAllViews();
            return;
        }
        for (BrandModel brandModel : brandModels) {
            View view = layoutInflater.inflate(R.layout.layout_index_brand_item, this, false);
            addView(view);
            LinearLayout.LayoutParams viewLp = (LayoutParams) view.getLayoutParams();
            viewLp.width = brandItemW;
            viewLp.height = LayoutParams.WRAP_CONTENT;
            view.setLayoutParams(viewLp);

            ImageView imageView = (ImageView) view.findViewById(R.id.iv_img);
            LinearLayout.LayoutParams ivLp = (LayoutParams) imageView.getLayoutParams();
            ivLp.width = brandImgItemW;
            ivLp.height = brandImgItemH;
            imageView.setLayoutParams(ivLp);

            TextView textView = (TextView) view.findViewById(R.id.tv_text);
            LinearLayout.LayoutParams tvLp = (LayoutParams) textView.getLayoutParams();
            tvLp.width = brandImgItemW;
            textView.setLayoutParams(tvLp);

            HCUtils.loadWebImg(mContext, imageView, brandModel.getBrand_img());
            textView.setText(brandModel.getBrand_name());

            view.setOnClickListener(this);
            view.setTag(brandModel);
        }

    }

    @Override
    public void onClick(View v) {
        if (v.getTag() == null) return;
        if (!(v.getTag() instanceof BrandModel)) return;
        BrandModel model = (BrandModel) v.getTag();
        if (model == null || listener == null) return;
        listener.onIndexBrandItemClick(model);
    }

    private IndexBrandClickListener listener;

    public void setListener(IndexBrandClickListener listener) {
        this.listener = listener;
    }
}
